package com.jzh.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jzh.dao.PermissionDao;
import com.jzh.entity.PageResult;
import com.jzh.entity.QueryPageBean;
import com.jzh.pojo.CheckItem;
import com.jzh.pojo.Permission;
import com.jzh.service.PermissionService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限管理实现
 * @author jzh971018
 */
@Service(version = "1.0.0")
@Transactional
public class PermissionServiceImpl implements PermissionService{

    @Resource
    private PermissionDao permissionDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        //查询条件
        String queryString = queryPageBean.getQueryString();
        //完成分页查询，基于mybatis框架提供的分页助手插件完成
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> page = permissionDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    @Override
    public void deleteById(Integer id) {
        //判断当前权限是否已经关联到检查组
        long count  = permissionDao.findCountByPermissionId(id);
        if (count > 0){
            //当前权限已经被关联到角色中，不允许删除
            new RuntimeException();
        }
        permissionDao.deleteById(id);
    }
    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

    @Override
    public void removeBatch(Integer[] ids) {
        if (ids != null && ids.length > 0){
            for (Integer id : ids) {
                permissionDao.deleteById(id);
            }
        }
    }
}
